<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <TreeTool :data="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="departsProps" :default-expand-all="true">
          <TreeTool slot-scope="{data}" :data="data" @delDepts="getDepartments" @editDepts="editDepts" @addDepts="addDepts" />

        </el-tree>
      </el-card>

      <addDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import addDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import TreeTool from './components/tree-tools.vue'
import { tranListToTreeData } from '@/utils'

export default {
  components: {
    TreeTool,
    addDept
  },
  data() {
    return {
      loading: false,
      departsProps: {
        label: 'name'
      },
      company: {},
      departs: [],
      showDialog: false,
      node: {}// 当前点击节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { depts, companyName } = await getDepartments()
      this.departs = tranListToTreeData(depts, '')
      this.company = {
        name: companyName,
        manager: '负责人',
        id: ''

      }
      this.loading = false
    },
    addDepts(data) {
      this.showDialog = true
      this.node = data
    },
    async editDepts(node) {
      this.node = node
      this.showDialog = true
      // 父组件 调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }

  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
